#include "util.h"
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <cstdlib>

int64_t write_to_file(int fd, void* buff, size_t len)
{
    int  ret;
    struct stat st;
    ret = fstat(fd, &st);
    return_val_if_fail(ret != -1, -1);

    char* mm;
    mm = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    return_val_if_fail(mm != NULL, -1);
    memcpy(mm , buff, len);
    munmap(mm, st.st_size);
    return len;
}
int64_t read_from_file(int fd, void* buff, size_t len)
{
    int  ret;
    struct stat st;
    ret = fstat(fd, &st);
    return_val_if_fail(ret == 0, -1);
    char* mm;
    mm = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd ,0);
    return_val_if_fail(mm != NULL, -1);
    memcpy(buff, mm, len);
    munmap(mm, st.st_size);
    return len;
}


